using System.Collections.Generic;
using UnityEngine;

public class Audio : MonoBehaviour
{
	public static Audio instance;

	public static GameObject parentObject;

	private static bool _isEnabledSound;

	private static bool _isEnabledMusic;

	public static GameObject followTarget;

	[Header("Music")]
	public AudioClip soundMusicMenu;

	public float soundMusicMenuVolume = 1f;

	public AudioClip soundMusicGame;

	public float soundMusicGameVolume = 1f;

	[Header("Game Sounds")]
	public AudioClip soundRollingFloor;

	public float soundRollingFloorVolume = 1f;

	public AudioClip soundRollingPipe;

	public float soundRollingPipeVolume = 1f;

	public AudioClip soundCollisionFloor;

	public float soundCollisionFloorVolume = 1f;

	public AudioClip soundCollisionPipe;

	public float soundCollisionPipeVolume = 1f;

	public AudioClip soundCollisionObject;

	public float soundCollisionObjectVolume = 1f;

	public AudioClip soundGameButton;

	public float soundGameButtonVolume = 1f;

	public AudioClip soundTrampoline;

	public float soundTrampolineVolume = 1f;

	public AudioClip soundRespawn;

	public float soundRespawnVolume = 1f;

	public AudioClip soundLevelFinish;

	public float soundLevelFinishVolume = 1f;

	public AudioClip soundCheckpoint;

	public float soundCheckpointVolume = 1f;

	[Header("Menu Sounds")]
	public AudioClip soundButton;

	public float soundButtonVolume = 1f;

	public AudioClip soundPoint;

	public float soundPointVolume = 1f;

	public AudioClip soundLife;

	public float soundLifeVolume = 1f;

	public AudioClip soundLifeMinus;

	public float soundLifeMinusVolume = 1f;

	public AudioClip soundWildcard;

	public float soundWildcardVolume = 1f;

	public AudioClip soundWindowOpen;

	public float soundWindowOpenVolume = 1f;

	public AudioClip soundWindowClose;

	public float soundWindowCloseVolume = 1f;

	public AudioClip soundUnlockLevel;

	public float soundUnlockLevelVolume = 1f;

	private Dictionary<string, float> volumeScales;

	private float musicVolumeCurrent;

	private float musicVolumeTarget;

	private float musicVolumeFadeSpeed;

	private MusicMode musicModeCurrent;

	private float[] musicGameSeekTimes = new float[4] { 0f, 31.4f, 75f, 129.37f };

	private AudioListener listener;

	private AudioSource sourceMusic;

	private AudioSource sourceBallRolling;

	private AudioSource sourceFx;

	public static bool isEnabledSound
	{
		get
		{
			return _isEnabledSound;
		}
		set
		{
			_isEnabledSound = value;
			UserPrefs.setBool("isEnabledSound", _isEnabledSound);
		}
	}

	public static bool isEnabledMusic
	{
		get
		{
			return _isEnabledMusic;
		}
		set
		{
			_isEnabledMusic = value;
			UserPrefs.setBool("isEnabledMusic", _isEnabledMusic);
		}
	}

	public static float rollingSoundVolume
	{
		get
		{
			if (instance == null)
			{
				return 0f;
			}
			return instance.sourceBallRolling.volume;
		}
	}

	public static float rollingSoundPitch
	{
		get
		{
			if (instance == null)
			{
				return 1f;
			}
			return instance.sourceBallRolling.pitch;
		}
	}

	public static Audio GetInstance()
	{
		if (!instance || !parentObject)
		{
			parentObject = GameObject.Find("Audio");
			if (parentObject != null)
			{
				instance = parentObject.GetComponent<Audio>();
			}
			else
			{
				Debug.Log("NO AUDIO !");
			}
		}
		return instance;
	}

	public static float GetClipVolumeScale(AudioClip clip)
	{
		if ((bool)instance && instance.volumeScales.ContainsKey(clip.name))
		{
			return instance.volumeScales[clip.name];
		}
		return 1f;
	}

	public static void ToggleSound()
	{
		if (!(instance == null))
		{
			isEnabledSound = !isEnabledSound;
		}
	}

	public static void ToggleMusic()
	{
		if (instance == null)
		{
			return;
		}
		isEnabledMusic = !isEnabledMusic;
		if (isEnabledMusic)
		{
			if (Globals.appState == AppState.menu)
			{
				instance.DoStartMusic(MusicMode.menu);
			}
			else if (Globals.appState == AppState.game)
			{
				instance.DoStartMusic(MusicMode.game);
			}
			else
			{
				instance.DoStartMusic(MusicMode.unknown);
			}
		}
		else
		{
			instance.DoStopMusic();
		}
	}

	public static void StartMusic(MusicMode musicMode, float volumeSpeed = 2f)
	{
		if (!(instance == null) && isEnabledMusic)
		{
			instance.DoStartMusic(musicMode, volumeSpeed);
		}
	}

	public static void StopMusic(float volumeSpeed = 2f)
	{
		if (!(instance == null) && isEnabledMusic)
		{
			instance.DoStopMusic(volumeSpeed);
		}
	}

	public static void SetRollingSoundValues(float volume, float pitch, AudioSource source = null)
	{
		if (!(instance == null) && isEnabledSound)
		{
			if (source == null)
			{
				source = instance.sourceBallRolling;
			}
			if (source.isPlaying)
			{
				source.volume = volume * GetClipVolumeScale(source.clip);
				source.pitch = pitch;
			}
		}
	}

	public static void SetRollingSoundMode(RollingMode rollingMode, AudioSource source = null)
	{
		if (instance == null)
		{
			return;
		}
		if (source == null)
		{
			source = instance.sourceBallRolling;
		}
		switch (rollingMode)
		{
		case RollingMode.floor:
			if (source.clip != instance.soundRollingFloor)
			{
				source.clip = instance.soundRollingFloor;
			}
			break;
		case RollingMode.pipe:
			if (source.clip != instance.soundRollingPipe)
			{
				source.clip = instance.soundRollingPipe;
			}
			break;
		}
		if (source.clip == null)
		{
			source.clip = instance.soundRollingFloor;
		}
		if (isEnabledSound)
		{
			if (!source.isPlaying)
			{
				source.Play();
			}
		}
		else
		{
			source.Stop();
		}
	}

	public static void StopRollingSound(AudioSource source = null)
	{
		if (!(instance == null))
		{
			if (source == null)
			{
				source = instance.sourceBallRolling;
			}
			source.volume = 0f;
			source.Stop();
		}
	}

	public static void PlaySoundCollisionFloor(float volume, AudioSource source = null)
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundCollisionFloor, volume, source);
		}
	}

	public static void PlaySoundCollisionPipe(float volume, AudioSource source = null)
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundCollisionPipe, volume, source);
		}
	}

	public static void PlaySoundCollisionObject(float volume, AudioSource source = null)
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundCollisionObject, volume, source);
		}
	}

	public static void PlaySoundGameButton(AudioSource source)
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundGameButton, 1f, source);
		}
	}

	public static void PlaySoundTrampoline(AudioSource source)
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundTrampoline, 1f, source);
		}
	}

	public static void PlaySoundRespawn()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundRespawn, 1f);
		}
	}

	public static void PlaySoundLevelFinish()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundLevelFinish, 1f);
		}
	}

	public static void PlaySoundCheckpoint()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundCheckpoint, 1f);
		}
	}

	public static void PlaySoundButton()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundButton, 1f);
		}
	}

	public static void PlaySoundPoint()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundPoint, 1f);
		}
	}

	public static void PlaySoundWildcard()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundWildcard, 1f);
		}
	}

	public static void PlaySoundWindowOpen()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundWindowOpen, 1f);
		}
	}

	public static void PlaySoundWindowClose()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundWindowClose, 1f);
		}
	}

	public static void PlaySoundUnlockLevel()
	{
		if (!(instance == null))
		{
			PlayShotSound(instance.soundUnlockLevel, 1f);
		}
	}

	public static void PlayShotSound(AudioClip clip, float volume = 1f, AudioSource source = null)
	{
		if (instance == null || !isEnabledSound)
		{
			return;
		}
		if (clip == null)
		{
			Debug.Log("WTF: Audio clip is NULL");
			return;
		}
		float clipVolumeScale = GetClipVolumeScale(clip);
		if (source == null)
		{
			instance.sourceFx.PlayOneShot(clip, clipVolumeScale * volume);
		}
		else
		{
			source.PlayOneShot(clip, clipVolumeScale * volume);
		}
	}

	private void Awake()
	{
		if ((bool)GameObject.Find("Audio (indestructible)"))
		{
			Object.Destroy(base.gameObject);
		}
	}

	private void Start()
	{
		GetInstance();
		Object.DontDestroyOnLoad(base.gameObject);
		base.name = "Audio (indestructible)";
		isEnabledSound = UserPrefs.getBool("isEnabledSound", true);
		isEnabledMusic = UserPrefs.getBool("isEnabledMusic", true);
		volumeScales = new Dictionary<string, float>();
		volumeScales[soundMusicMenu.name] = soundMusicMenuVolume;
		volumeScales[soundMusicGame.name] = soundMusicGameVolume;
		volumeScales[soundRollingFloor.name] = soundRollingFloorVolume;
		volumeScales[soundRollingPipe.name] = soundRollingPipeVolume;
		volumeScales[soundCollisionFloor.name] = soundCollisionFloorVolume;
		volumeScales[soundCollisionPipe.name] = soundCollisionPipeVolume;
		volumeScales[soundCollisionObject.name] = soundCollisionObjectVolume;
		volumeScales[soundGameButton.name] = soundGameButtonVolume;
		volumeScales[soundTrampoline.name] = soundTrampolineVolume;
		volumeScales[soundRespawn.name] = soundRespawnVolume;
		volumeScales[soundLevelFinish.name] = soundLevelFinishVolume;
		volumeScales[soundCheckpoint.name] = soundCheckpointVolume;
		volumeScales[soundButton.name] = soundButtonVolume;
		volumeScales[soundPoint.name] = soundPointVolume;
		volumeScales[soundLife.name] = soundLifeVolume;
		volumeScales[soundLifeMinus.name] = soundLifeMinusVolume;
		volumeScales[soundWildcard.name] = soundWildcardVolume;
		volumeScales[soundWindowOpen.name] = soundWindowOpenVolume;
		volumeScales[soundWindowClose.name] = soundWindowCloseVolume;
		volumeScales[soundUnlockLevel.name] = soundUnlockLevelVolume;
		listener = base.gameObject.AddComponent<AudioListener>();
		listener.enabled = true;
		sourceMusic = base.gameObject.AddComponent<AudioSource>();
		sourceBallRolling = base.gameObject.AddComponent<AudioSource>();
		sourceFx = base.gameObject.AddComponent<AudioSource>();
		sourceMusic.clip = soundMusicMenu;
		sourceMusic.loop = true;
		sourceMusic.volume = 0f;
		sourceBallRolling.volume = 0f;
		sourceBallRolling.loop = true;
	}

	private void Update()
	{
		if ((bool)followTarget)
		{
			base.transform.position = followTarget.transform.position;
		}
		if (musicVolumeCurrent != musicVolumeTarget)
		{
			musicVolumeCurrent = Mathf.MoveTowards(musicVolumeCurrent, musicVolumeTarget, musicVolumeFadeSpeed * Time.unscaledDeltaTime);
			sourceMusic.volume = musicVolumeCurrent;
		}
	}

	private void DoStartMusic(MusicMode musicMode, float volumeSpeed = 2f)
	{
		if (!sourceMusic.isPlaying)
		{
			sourceMusic.Play();
		}
		float num = 1f;
		if (musicMode != 0 && musicMode != musicModeCurrent)
		{
			musicModeCurrent = musicMode;
			if (musicModeCurrent == MusicMode.menu)
			{
				sourceMusic.clip = soundMusicMenu;
				sourceMusic.time = 0f;
			}
			else if (musicModeCurrent == MusicMode.game)
			{
				sourceMusic.clip = soundMusicGame;
				sourceMusic.time = musicGameSeekTimes[Random.Range(0, 4)];
			}
			sourceMusic.Play();
		}
		num *= GetClipVolumeScale(sourceMusic.clip);
		musicVolumeTarget = num;
		musicVolumeFadeSpeed = volumeSpeed;
	}

	private void DoStopMusic(float volumeSpeed = 2f)
	{
		musicVolumeTarget = 0f;
		musicVolumeFadeSpeed = volumeSpeed;
	}
}
